package com.woniuxy.config;

import com.woniuxy.controller.ChatHandler;
import com.woniuxy.controller.NotificationHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;


@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final ChatHandler chatHandler;
    private final NotificationHandler notificationHandler; // 添加通知处理器
    // 通过构造函数注入两个处理器
    public WebSocketConfig(ChatHandler chatHandler, NotificationHandler notificationHandler) {
        this.chatHandler = chatHandler;
        this.notificationHandler = notificationHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket处理器，允许跨域
        registry.addHandler(chatHandler, "/ws/chat/{receiverId}/{userId}")
                .setAllowedOrigins("*");

        // 新增：注册通知WebSocket处理器
        registry.addHandler(notificationHandler, "/ws/notifications/{userId}")
                .setAllowedOrigins("*");
    }
}